home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CADSP 1.0 / Messenger Classes / CMessenger.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  5.4 KB  |  339 lines  |  [TEXT/KAHL]

  1. /***
  2.  * CMessenger
  3.  *
  4.  *        High level ADSP handler
  5.  *
  6.  *        this object handles data stream connections over appletalk
  7.  *        
  8.  *    Superclass: CADSP
  9.  *
  10.  *        Copyright © 1992 Bernard Bernstein. All rights reserved.
  11.  ***/ 
  12.  
  13. #include <Global.h>
  14.  
  15. #include "CMessenger.h"
  16.  
  17. #define QSIZE        600
  18. #define DATASIZE    128
  19.  
  20. /***
  21.  * IMessenger
  22.  *
  23.  *        initialize the messenger
  24.  ***/
  25. void CMessenger::IMessenger(void)
  26. {
  27.     CNBP *anNBP = nil;
  28.  
  29.     itsSocket = 0;
  30.     waitingForConnect = false;
  31.     named = false;
  32.  
  33.     // make an NBP handler for name binding
  34.     anNBP = new CNBP;
  35.     anNBP->INBP();
  36.     itsNBP = anNBP;
  37.     
  38.     anNBP->SetSelfSend(true);
  39.  
  40.     CADSP::IADSP(QSIZE, DATASIZE);
  41.     
  42. }
  43.  
  44.  
  45. /***
  46.  * Dispose
  47.  *
  48.  *        Kill the messenger
  49.  ***/
  50. void CMessenger::Dispose(void)
  51. {
  52.     KillConnector();
  53.     ForgetObject(itsNBP);
  54.     
  55.     inherited::Dispose();
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62. /**********************************************************************
  63.  
  64.                         Accessing information
  65.             
  66. **********************************************************************/
  67.  
  68.  
  69. /***
  70.  * GetSocket
  71.  *
  72.  *        return the socket number for this session
  73.  ***/
  74. short CMessenger::GetSocket(void)
  75. {
  76.     return itsSocket;
  77. }
  78.  
  79. /***
  80.  * GetRemoteAddr
  81.  *
  82.  *        return the AddrBlock of the remote connection which contacted
  83.  *        this server.
  84.  ***/
  85. AddrBlock CMessenger::GetRemoteAddr(void)
  86. {
  87.     return itsRemoteAddr;
  88. }
  89.  
  90.  
  91. /***
  92.  * GetNBP
  93.  *
  94.  *        return the nbp handler
  95.  ***/
  96. CNBP *CMessenger::GetNBP(void)
  97. {
  98.     return itsNBP;
  99. }
  100.  
  101.  
  102.  
  103.  
  104. /***
  105.  * SetSocket
  106.  *
  107.  *        set the socket for this stream
  108.  ***/
  109. void CMessenger::SetSocket(short aSocket)
  110. {
  111.     itsSocket = aSocket;
  112.     itsNBP->SetSocket(aSocket);
  113. }
  114.  
  115. /***
  116.  * SetRemoteAddr
  117.  *
  118.  *        set the remote address for this stream
  119.  ***/
  120. void CMessenger::SetRemoteAddr(AddrBlock anAddr)
  121. {
  122.     itsRemoteAddr = anAddr;
  123. }
  124.  
  125.  
  126.  
  127.  
  128. /**********************************************************************
  129.  
  130.                             Name binding stuff
  131.             
  132. **********************************************************************/
  133.  
  134. /***
  135.  * SetName
  136.  *
  137.  *        Provide a name for this connection.
  138.  *        Needs an object name, type name and zone name
  139.  ***/
  140. void CMessenger::SetName(Str32 objName, Str32 typeName, Str32 zoneName)
  141. {
  142.     ASSERT(itsNBP);
  143.     
  144.     itsNBP->SetName(objName, typeName, zoneName);
  145.     named = true;
  146. }
  147.  
  148. /***
  149.  * PostName
  150.  *
  151.  *        Post the NBP name for the socket. A socket must have been established first.
  152.  ***/
  153. void CMessenger::PostName(void)
  154. {
  155.     ASSERT(itsNBP);
  156.     ASSERT(itsSocket != 0);
  157.     
  158.     // register this socket with NBP
  159.     itsNBP->Register();
  160. }
  161.  
  162. /***
  163.  * RemoveName
  164.  *
  165.  *        Remote the NBP name
  166.  ***/
  167. void CMessenger::RemoveName(void)
  168. {
  169.     ASSERT(itsNBP);
  170.     itsNBP->Remove();
  171.     named = false;
  172. }
  173.  
  174.  
  175.  
  176.  
  177. /**********************************************************************
  178.  
  179.                 Starting and Stopping a connection
  180.             
  181. **********************************************************************/
  182.  
  183.  
  184. /***
  185.  * MakeConnector
  186.  *
  187.  *        make a socket which can be used for connecting to another socket.
  188.  *
  189.  *        The messenger must be named before this method will work.
  190.  ***/
  191. void CMessenger::MakeConnector(void)
  192. {
  193.     short aSocket = itsSocket;
  194.  
  195.     ASSERT(named);
  196.     
  197.     DSPInit(&aSocket);            // if itsSocket was 0, it will be reassigned
  198.     SetSocket(aSocket);
  199.  
  200.     PostName();
  201. }
  202.  
  203.  
  204. /***
  205.  * KillConnector
  206.  *
  207.  *        remove the connector completely
  208.  ***/
  209. void CMessenger::KillConnector(void)
  210. {
  211.     RemoveName();
  212.     DSPRemove();
  213. }
  214.  
  215.  
  216. /***
  217.  * OpenSession
  218.  *
  219.  *        open an active session with another socket connector
  220.  *        the AddrBlock of the other session is passed in here
  221.  *
  222.  ***/
  223. void CMessenger::OpenSession(AddrBlock addr)
  224. {
  225.     // ••• do i have to close it if one is already open passively?
  226.     waitingForConnect = false;
  227.     DSPOpen(addr, ocRequest);
  228.     SetRemoteAddr(addr);
  229. }
  230.  
  231.  
  232. /***
  233.  * WaitForSession
  234.  *
  235.  *        open a passive session. waiting for another socket
  236.  *        to request a connection.
  237.  *
  238.  *        *** This call is made synchronously ***
  239.  *
  240.  *        after calling this method, you must periodically call:
  241.  *
  242.  *            
  243.  ***/
  244. void CMessenger::WaitForSession(void)
  245. {
  246.     AddrBlock filter;
  247.     
  248.     filter.aNet = 0;
  249.     filter.aNode = 0x00;
  250.     filter.aSocket = 0x00;
  251.     
  252.     DSPOpen(filter, ocPassive);
  253.     waitingForConnect = true;
  254. }
  255.  
  256. /***
  257.  * StillWaitingForSession
  258.  *
  259.  *        Call this periodically after calling WaitForSession.
  260.  *        if a session has been requested, it will return true.
  261.  *
  262.  ***/
  263. Boolean CMessenger::StillWaitingForSession(void)
  264. {
  265.     OSErr err;
  266.  
  267.     if (waitingForConnect && ((err = DSPioResult()) != 1))
  268.         {
  269.         waitingForConnect = false;
  270.         if (err != 1)
  271.             {
  272.             FailOSErr(err);
  273.             }
  274.         }
  275.     
  276.     return waitingForConnect;
  277. }
  278.  
  279.  
  280. /***
  281.  * CloseSession
  282.  *
  283.  *        Close the session
  284.  ***/
  285. void CMessenger::CloseSession(void)
  286. {
  287.     DSPClose();
  288.     waitingForConnect = false;
  289. }
  290.  
  291.  
  292. /**********************************************************************
  293.  
  294.                         Maintaining a connection
  295.             
  296. **********************************************************************/
  297.  
  298.  
  299. /***
  300.  * ReceiveMessage
  301.  *
  302.  *        Read some data from the connection
  303.  *        This should be done whenever MessageWaiting returns true
  304.  ***/
  305. void CMessenger::ReceiveMessage(void *message, short amountToRead, short *amountRead)
  306. {
  307.     DSPRead(message, amountToRead, amountRead);
  308. }
  309.  
  310.  
  311. /***
  312.  * SendMessage
  313.  *
  314.  *        Send some data across the connection
  315.  ***/
  316. void CMessenger::SendMessage(void *message, short size)
  317. {
  318.     DSPWrite(message, size);
  319. }
  320.  
  321.  
  322. /***
  323.  * MessageWaiting
  324.  *
  325.  *        Is there a message waiting?
  326.  *        If this is true, then do a ReceiveMessage to get it.
  327.  *        This should be called periodically to handle incoming messages.
  328.  ***/
  329. Boolean CMessenger::MessageWaiting(void)
  330. {
  331.     short sendPend, sendAvail, recvPend, recvAvail;
  332.     
  333.     DSPStatus(&sendPend, &sendAvail, &recvPend, &recvAvail);
  334.     
  335.     return (recvPend > 0);
  336. }
  337.  
  338.  
  339.